package com.luocc.tim.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.luocc.tim.MainActivity;
import com.luocc.tim.R;
import com.luocc.tim.entity.Message;
import com.luocc.tim.recycler.ChatRecordAdapter;
import com.luocc.tim.recycler.ChatRecordRecycler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChatInterfaceFragment extends Fragment {
    public static final String TAG = "ChatInterfaceFragment";
    private MainActivity activity;
    private String target;


    private TextView name;
    private ImageView back;
    private ImageView settings;
    private ChatRecordRecycler chatRecordRecycler;
    private ImageView record;
    private ImageView expression;
    private ImageView more;
    private EditText editText;
    private Button send;

    private View lastClicked;

    @Override
    public void onAttach(@NonNull Context context) {
        activity = (MainActivity) context;
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            target = arguments.getString("target");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.interface_chat, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initView(view);
    }

    private void initView(View root) {


        back = root.findViewById(R.id.chat_back);
        name = root.findViewById(R.id.chat_name);
        settings = root.findViewById(R.id.chat_settings);

        chatRecordRecycler = root.findViewById(R.id.chat_recycler);

        record = root.findViewById(R.id.chat_record);
        expression = root.findViewById(R.id.chat_expression);
        more = root.findViewById(R.id.chat_more);
        editText = root.findViewById(R.id.chat_edit);
        send = root.findViewById(R.id.chat_send);


        back.setOnClickListener(v -> {
            if (getActivity() != null) {
                getActivity().onBackPressed();
            }
        });

        settings.setOnClickListener(v -> {

        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        List<Message> messages = getMessage();
        ChatRecordAdapter chatRecordAdapter = new ChatRecordAdapter(activity, messages);
        chatRecordRecycler.setLayoutManager(linearLayoutManager);
        chatRecordRecycler.setAdapter(chatRecordAdapter);

        LinearSmoothScroller scroller = new LinearSmoothScroller(activity);

        record.setOnClickListener(v -> {
            controlButtonState(record);
            lastClicked = record;
            record.setImageResource(R.mipmap.record_on);
        });

        expression.setOnClickListener(v -> {
            controlButtonState(expression);
            lastClicked = expression;
            expression.setImageResource(R.mipmap.expression_on);
        });

        more.setOnClickListener(v -> {
            controlButtonState(more);
            lastClicked = more;
            more.setImageResource(R.mipmap.chat_more_on);
        });
        
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editText.getText().length() != 0) {
                    if (send.getVisibility() != View.VISIBLE) {
                        send.setVisibility(View.VISIBLE);
                        more.setVisibility(View.GONE);
                    }
                } else {
                    if (more.getVisibility() != View.VISIBLE) {
                        more.setVisibility(View.VISIBLE);
                        send.setVisibility(View.GONE);
                    }
                }
            }
        });

        send.setOnClickListener(v -> {
            Message message = new Message("luocc", target, editText.getText().toString(), new Date(System.currentTimeMillis()));
            chatRecordAdapter.addData(message);
            editText.setText("");
            // 这里的size大于等于的效果是一样的
            scroller.setTargetPosition(messages.size() - 1);
            linearLayoutManager.startSmoothScroll(scroller);
        });
        name.setText(target);
    }

    private List<Message> getMessage() {
        List<Message> messages = new ArrayList<>();
        Message message1 = new Message("zwb", "luocc", "你吃了没", new Date(System.currentTimeMillis()));
        Message message2 = new Message("zwb", "luocc", "你吃了没", new Date(System.currentTimeMillis()));
        Message message3 = new Message("zwb", "luocc", "你吃了没", new Date(System.currentTimeMillis()));
        Message message4 = new Message("luocc", "zwb", "不想吃，没啥好吃的", new Date(System.currentTimeMillis()));
        messages.add(message1);
        messages.add(message2);
        messages.add(message3);
        messages.add(message4);
        return messages;
    }

    /**
     * 同时只允许最多一个按钮处于开启状态
     */
    private void controlButtonState(View view) {
        if (view == lastClicked) {
            // 隐藏输入面板
        } else {
            // 恢复上一个按钮的状态
            closeOtherButton();
        }
    }

    private void closeOtherButton() {
        if (lastClicked == record) {
            record.setImageResource(R.mipmap.record_off);
        } else if (lastClicked == expression) {
            expression.setImageResource(R.mipmap.expression_off);
        } else if (lastClicked == more) {
            more.setBackgroundResource(R.mipmap.chat_more_off);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();
    }


}
